/*
 * LDIR
 *
 * LDIR, a GPS cartography application built for Let's Do It Romania eco
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 *
 * http://generasoftware.com/
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include "WasteCache.h"
#include "wasteinfo.h"

CWasteCache::CWasteCache()
	{
	// No implementation required
	}

CWasteCache::~CWasteCache()
	{
	iWasteCache.ResetAndDestroy();
	}

CWasteCache* CWasteCache::NewLC()
	{
	CWasteCache* self = new ( ELeave ) CWasteCache();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}

CWasteCache* CWasteCache::NewL()
	{
	CWasteCache* self = CWasteCache::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CWasteCache::ConstructL()
	{

	}

void CWasteCache::AppendL( CWasteInfo* aWasteInfo )
	{
	CWasteInfo* wasteInfo = GetById( aWasteInfo->iId );
	if (wasteInfo)
		{
		Remove(wasteInfo);
		delete wasteInfo;
		}
	iWasteCache.AppendL( aWasteInfo );
	}

void CWasteCache::ClearCache()
	{
	iWasteCache.ResetAndDestroy();
	}

void CWasteCache::Remove( TInt aPos )
	{
	delete iWasteCache[aPos];
	iWasteCache.Remove( aPos );
	}

void CWasteCache::Remove( CWasteInfo* aWasteInfo )
	{
	TInt pos = iWasteCache.Find( aWasteInfo );
	iWasteCache.Remove( pos );
	}

CWasteInfo* CWasteCache::GetById( const TDesC& aWasteId )
	{
	TInt count = iWasteCache.Count();
	for ( TInt i = 0; i < count; ++i )
		{
		if ( aWasteId.Compare( iWasteCache[i]->iId ) == 0 )
			{
			return iWasteCache[i];
			}
		}
	return NULL;
	}

CWasteInfo* CWasteCache::operator []( const TInt aIndex ) const
	{
	return iWasteCache[aIndex];
	}

TInt CWasteCache::Count()
	{
	return iWasteCache.Count();
	}

void CWasteCache::ExternalizeL( RWriteStream& aStream ) const
	{
	TInt count = iWasteCache.Count();
	aStream.WriteInt32L( count );
	for ( TInt i = 0; i < count; ++i )
		{
		aStream << *iWasteCache[i];
		}
	}

void CWasteCache::InternalizeL( RReadStream& aStream )
	{
	CWasteInfo* info = NULL;
	TInt count = aStream.ReadInt32L();
	for ( TInt i = 0; i < count; ++i )
		{
		info = CWasteInfo::NewLC();
		aStream >> *info;
		iWasteCache.AppendL( info );
		CleanupStack::Pop( info );
		}
	}

HBufC8* CWasteCache::EncodeL( CJsonGenerator& aJsonGenerator )
	{
	delete iEntity;
	iEntity = NULL;
	iEntity = CEntity::NewL( array_type );

	for ( TInt i = 0; i < iWasteCache.Count(); ++i )
		{
		iWasteCache[i]->ExternalizeToEntityL();
		AppendEntityL( *( iWasteCache[i]->Entity() ) );
		}

	HBufC8* retBuff = CWsObject::EncodeL( aJsonGenerator );
	delete iEntity;
	iEntity = NULL;
	return retBuff;
	}

void CWasteCache::DecodeL( const TDesC8& aDesC, CJsonParser& aJsonParser )
	{
	CWsObject::DecodeL( aDesC, aJsonParser );

	//    CEntity* entity = NULL;
	CPair* pair = NULL;
	CEntity* pairValue = NULL;
	TInt index = 0;

	//	if ( aEntities == NULL )
	//		return;
	//	do
	//		{
	//		//        GetNextPair(aEntities, pair, index);
	//		if ( index != -1 )
	//			{
	//			pairValue = pair->iValue;
	//			if ( pairValue != NULL )
	//				{
	//				switch ( pair->iFieldIndex )
	//					{
	//					case FieldsStringTable::EFldPhoneNumber:
	//						if ( iNumber )
	//							delete iNumber;
	//						*iNumber = pairValue->GetStr();
	//						break;
	//					case FieldsStringTable::EFldLabel:
	//						if ( iLabel )
	//							delete iLabel;
	//						*iLabel = pairValue->GetStr();
	//						break;
	//					case FieldsStringTable::EFldOrder:
	//						iOrder = pairValue->GetInt();
	//						break;
	//					case FieldsStringTable::EFldAssociatedUserId:
	//						iAssociatedUserId = pairValue->GetInt();
	//						break;
	//					case FieldsStringTable::EFldEnumStatus:
	//						iEnumStatus = ( TPhoneType ) pairValue->GetInt();
	//						break;
	//					}
	//				}
	//			}
	//
	//		}
	//	while ( index != -1 );
	delete iEntity;
	iEntity = NULL;
	}
